An array is a variable that can hold multiple values of the same data type simultaneously. Arrays are built into the core of both C and C++. It is useful when you have to declare many variables. Instead of declaring individual variables such as a0, a1, a2, … a99, you declare one array variable such as int a[100] and use a[0], a[1],a[2],….a[99] to represent individual variables.
To access an individual element in an array, you need to use an index. An index is a unique identifier to identify each element of an array. It always starts at zero. To access the first element of an array you have to use array_name[0]. array_name[1] has 2nd element, array_name[2] has 3rd element and so on. The last element will be available on the index that is equal to the size of the array minus 1.
For example
int students_id[3]; // This would declare an array of 3 integer students_id[0] = 1; student_id[1] = 2; student_id[2] = 3;
This array holds 3 integer values.
How to reserve an array in C
There are two methods to reverse an array: the iterative method and the recursion method.
Iterative method
Let’s say, you have an array of an integer data type.
int data = {1,2,3,4,5,6};
To reverse an array, in an iterative manner, simply swap the first element with the last element, the second element with the second last and so on.
Create a new project in CodeBlocks or your favorite IDE to write C programs.
Create a new file and write the following piece of code. Save the file as “reverseArray.c”. Compile and Run to see the output.
#include <stdio.h> void reverseArray(int arr[], int length); int main(){ int length; //Take total size of the array by user printf("Please enter the number of elements in the array : "); scanf("%d", &length); int arr[length]; //Take array elements from the user input printf("Enter array elements\n"); for (int i = 0; i < length; i++){ scanf("%d", &arr[i]); }//for loop reverseArray(arr, length); printf("After reversing an array\n"); for (int j = 0; j < len ; j++){ printf("%d ", arr[j]); } //for loop } //main void reverseArray(int arr[], int len){ int start =0, end=length-1; int temp; while (start < end) { temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; start++; end--; } }
Recursion method
The following code will reverse an array using recursion method.
#include <stdio.h> void reverseArray(int arr[], int start, int end) ; int main(){ int length, start=0,end; //Take total size of the array by user printf("Please enter the number of elements in the array : "); scanf("%d", &length); int arr[length]; end = length-1; //Take array elements from the user printf("Enter array elements\n"); for (int i = 0; i < length; i++){ scanf("%d", &arr[i]); } //for loop reverseArray( arr, start, end) ; printf("After reversing an array\n"); for (int j = 0; j < length; j++){ printf("%d ", arr[j]); }//for loop }//main void reverseArray(int arr[], int start, int end) { int temp; if (start >= end) return; temp = arr[start]; arr[start] = arr[end]; arr[end] = temp; reverseArray(arr, start+1, end-1); }
Related Articles
Comments